import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// import register from "../pages/register/register.vue"
const register = () => {
    console.log("=========register=============")
    return Promise.resolve(
        import ("../pages/register/register.vue"))
}

// import login from "../pages/login/login.vue"
const login = () =>
    import ("../pages/login/login.vue")
    // import index from "../pages/index/index.vue"

// import list from "../pages/list/list.vue"
// import detail from "../pages/detail/detail.vue"
// import search from "../pages/search/search.vue"
// import test from "../pages/test/test.vue"

// import home from "../pages/home/home.vue"
// import cate from "../pages/cate/cate.vue"
// import shop from "../pages/shop/shop.vue"
// import mine from "../pages/mine/mine.vue"
const routes = [{
        path: "/register",
        component: register,
        // component:()=>{}
        meta: {
            title: "注册",
            back: true
        }
    },
    {
        path: "/login",
        components: {
            default: login,
            second: () =>
                import ("../pages/test/test.vue")
        },
        //元信息
        meta: {
            title: "登录",
            register: true
        }
    },
    {
        path: "/index",
        component: () =>
            import ("../pages/index/index.vue"),

        children: [{
                path: "home",
                component: () =>
                    import ("../pages/home/home.vue"),
                meta: {
                    title: "首页"
                }
            },
            {
                path: "cate",
                component: () => Promise.resolve(
                    import ("../pages/cate/cate.vue")),
                meta: {
                    title: "分类"
                }
            },
            {
                path: "shop",
                component: () =>
                    import ("../pages/shop/shop.vue"),
                meta: { title: "购物车" }
            },
            {
                path: "mine",
                component: () =>
                    import ("../pages/mine/mine.vue"),
                meta: { title: "个人中心" }
            },
        ]
    },
    {
        path: "/list",
        component: () =>
            import ("../pages/list/list.vue"),
        name: "列表",
        meta: {
            title: "商品列表",
            back: true
        }
    },
    {
        path: "/detail/:id",
        name: "详情",
        component: () =>
            import ("../pages/detail/detail.vue"),
        meta: {
            title: "商品详情",
            back: true
        },
    },
    {
        path: "/search",
        // 别名
        alias: "/s",
        name: "搜索", //命名路由
        component: () =>
            import ("../pages/search/search.vue"),
        meta: {
            title: "搜索",
            back: true
        }
    }, {
        path: "/a-add",
        component: () =>
            import ("../pages/address_add/add.vue"),
        meta: {
            title: "地址编辑",
            back: true
        }
    },
    {
        path: "/order",
        component: () =>
            import ("../pages/order/order.vue"),
        meta: {
            title: "确认订单",
            back: true
        }
    },
    {
        path: "/pay",
        component: () =>
            import ("../pages/pay/pay.vue"),
        meta: {
            title: "订单支付",
            back: true
        }
    },
    {
        path: "/a-list",
        component: () =>
            import ("../pages/address_list/list.vue"),
        meta: {
            title: "地址管理",
            back: true
        }
    },
    {
        path: "*",
        redirect: "/login"
    }
]

const router = new VueRouter({
    //路由模式：hash(默认)[localhost:8080/#/login]  history-[localhost:8080/login]
    mode: "hash",
    // 路由规则
    routes,
    // 滚动行为
    // 希望每进入一个路由，如果之前没有访问过，从(0,0)开始显示；
    // 如果之前访问过，就要从上一次离开的额位置访问
    /**
     * 
     * @param {*} to 前往新的 的路由信息 $route
     * @param {*} from 从哪里来的路由信息 $route
     * @param {*} savePosition 之前没有访问过这个路由，就是null;如果之前访问过，那么他就是之前离开的时候的坐标 {x:0,y:166}
     * return   返回的是一进来的时候访问的坐标位置
     */
    scrollBehavior(to, from, savePosition) {
        return savePosition ? savePosition : { x: 0, y: 0 }
    }

})

// 全局路由守卫
/**进入每一个路由之前
 * 
 * @param {*} to 前往新的 的路由信息 $route
 * @param {*} from 从哪里来的路由信息 $route
 * @param {*} next 是否允许进入。 允许就调用next(),不调用，就不会进入路由
 
 */
router.beforeEach((to, from, next) => {

    if (to.path == '/login' || to.path == '/register') {
        next()
        return;
    }
    let isLogin = localStorage.getItem("isLogin"); // null '1'
    if (isLogin) {
        next()
    } else {
        next("/login")
    }

})




export default router